#!/bin/sh
# PCP QA Test No. 761
# Exercise the PMCD privileged co-process (root) PMDA
# Install, Remove and handling of different container
# engines container.* metrics.
#
# Copyright (c) 2014-2015 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

case "$PCP_PLATFORM"
in
    linux)
    	;;
    *)
	_notrun "No container support for PCP_PLATFORM $PCP_PLATFORM"
	# NOTREACHED
	;;
esac


_get_libpcp_config
$unix_domain_sockets || _notrun "No unix domain socket support available"

root=$tmp.root
status=1	# failure is the default!

root_cleanup()
{
    cd $here
    [ -d $root ] && $sudo rm -fr $root
    [ -f $tmp.conf.backup ] && $sudo cp $tmp.conf.backup $PCP_DIR/etc/pcp.conf
    _restore_pmda_install root || _exit 1
    rm -f $here/$seq.test-lxc-info.sh
    rm -rf $tmp.* $tmp
}

# do metrics one at a time, map all internal instance ids to N and sort
# instance lines
#
_check()
{
    pminfo containers | LC_COLLATE=POSIX sort \
    | while read metric
    do
	pminfo -f $metric >$tmp.tmp
	grep -v ' inst \[' $tmp.tmp
	sed -n -e '/inst \[/s/\[[0-9][0-9]*/[N/p' $tmp.tmp \
	| LC_COLLATE=POSIX sort
    done
}

_prepare_pmda root containers
trap "root_cleanup; exit \$status" 0 1 2 3 15

# backup main PCP config
cp $PCP_DIR/etc/pcp.conf $tmp.conf
cp $PCP_DIR/etc/pcp.conf $tmp.conf.backup

# simple testing script for LXC containers
cat >$here/$seq.test-lxc-info.sh <<EOF
#!/bin/sh
name="\$2"
test -d "\$PCP_LXC_DIR/\$name" || exit 0
echo "Name:           \$name"
echo "State:          RUNNING"
echo "PID:            17297"
echo "CPU use:        1.13 seconds"
EOF
chmod 755 $here/$seq.test-lxc-info.sh
export PCP_LXC_DIR=$root/var/lib/lxc

# real QA test starts here
echo zero | tee -a $seq_full
if ! _service pmcd status >>$seq_full; then _exit 1; fi
# systemd needs may not have caught up with a previous stop
# and the stop that comes below can cause a
#	Job for pmcd.service canceled.
# message.  So sleep a little in the hope that this fixes it ...
#
sleep 2
if ! _service pmcd status >>$seq_full; then _exit 1; fi
echo one | tee -a $seq_full
if ! _service pmcd stop; then _exit 1; fi \
| _filter_pcp_stop
echo two | tee -a $seq_full
if ! _service pmcd status >>$seq_full; then _exit 1; fi
_wait_pmcd_end || _exit 1
echo three | tee -a $seq_full

echo >> $tmp.conf
echo "# from QA $seq ..." >> $tmp.conf
echo PCP_LXC_DIR=$root/var/lib/lxc >> $tmp.conf
echo PCP_LXC_INFO=$here/$seq.test-lxc-info.sh >> $tmp.conf
echo PCP_DOCKER_DIR=$root/var/lib/docker >> $tmp.conf
echo PCP_SYSTEMD_CGROUP=/system.slice >> $tmp.conf
echo PCP_PODMAN_RUNDIR=$root/var/run/containers >> $tmp.conf
echo PCP_PODMAN_DATADIR=$root/var/lib/containers >> $tmp.conf
$sudo cp $tmp.conf $PCP_DIR/etc/pcp.conf
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1
echo four | tee -a $seq_full
if ! _service pmcd status >>$seq_full; then _exit 1; fi
_wait_for_pmcd || _exit 1
_wait_for_pmlogger || _exit 1

echo "=== Checking empty containers configuration, no values"
pmprobe -v containers | LC_COLLATE=POSIX sort
echo "== done" && echo

tarballs=`echo $here/linux/containers-*.tgz | LC_COLLATE=POSIX sort`
for tgz in $tarballs
do
    $sudo rm -fr $root
    mkdir $root || _fail "root in use when processing $tgz"
    cd $root
    $sudo tar xzf $tgz
    base=`basename $tgz`

    echo "== Checking values for active containers in $base"
    _check
    echo && echo "== done" && echo
    cd $here
done

# success, all done
status=0
exit
